window.onload = function () {
    document.getElementById("communication-deleted").className = "add_class";
    document.getElementById("correspondence").className = "add-nav-active";
    inbox_initialize();
};


function inbox_initialize(page=1,query_type='',query_value='') {
    document.getElementById("loading").style.display='inline-block';
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/correspondence/deleted.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("page="+String(page));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                window.JSONdetail = JsonDate["data"]["user_li"];
                window.page = page;
                create_table(JsonDate["data"]["total_count"], JsonDate["data"]["inbox_li"]);
                pagenum(JsonDate["data"]["total_count"], page,query_type,query_value);
            }
        }
    };
}

// 动态创建列表
function create_table(count_num, json_data) {
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");

        td.innerHTML = "<input id='"+json_data[i]["id"]+"' class='check_box' type=\"checkbox\">";td.style.background="#f8f9fa";td.style.textAlign = "center";
        td2.innerHTML = json_data[i]["sender_name"];td2.title = json_data[i]["sender_name"];td2.style.textAlign = "center";
        td3.innerHTML = json_data[i]["recipient_name"];td3.title = json_data[i]["recipient_name"];td3.style.textAlign = "center";
        td4.innerHTML = '<a href="delete_detail?message_id='+json_data[i]["message_id"]+'" onclick="RenderInboxDetail('+i+')" id="'+i+'" style="margin-left: 1%;" >'+json_data[i]["title"]+'</a>';td4.title = json_data[i]["title"];td4.className='username';
        td5.innerHTML = json_data[i]["create_time"];td4.title = json_data[i]["create_time"];td5.style.textAlign = "center";

        tr.appendChild(td);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);
        t_table.appendChild(tr);
    }
    document.getElementById("loading").style.display='none';
}

// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}

// 加载分页条
function pagenum(dataTotals=0, page=1,query_type,query_value) {
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            // var query_type = '';
            // var query_value ='';
            inbox_initialize(page,query_type,query_value);
            document.getElementById("check_all").checked = false;
        }
    });
}

// 全选与反全选
function check_all_not() {
    var check_box_list = document.getElementsByClassName("check_box");
    if (document.getElementById("check_all").checked) {
        for (var t=0; t<check_box_list.length; t++) {
            check_box_list[t].checked = true;
        }
    }else {
        for (var j=0; j<check_box_list.length; j++) {
            check_box_list[j].checked = false;
        }
    }
}

// 确认删除
function ConfirmDelete() {
    var myArray = new Array();
    var check_box_list = document.getElementsByClassName("check_box");
    var tb_list = document.getElementsByClassName("username");
    for (var t=0; t<check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(check_box_list[t].id)
        }
    }
    if (myArray.length === 0){
        document.getElementById("Confirm-content").innerHTML = "没有选择删除的信件！";
    }else {
        document.getElementById("Confirm-content").innerHTML = "确认要删除这"+String(myArray.length)+"条信件吗？"
    }
}

// 批量删除数据
function DeleteData() {
    var myArray = []
    var check_box_list = document.getElementsByClassName("check_box");
    for (var t=0; t<check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(check_box_list[t].id)
        }
    }
    if (myArray.length === 0){
        document.getElementById("close-model2").click();
        return myArray
    }
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/correspondence/completely_erase", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("delete_list="+String(myArray)+"&page=1");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("close-model2").click();
                window.location.reload();
            }
        }
    };
}


